<template>
  <el-dialog
    title="车辆配备申请"
    ref="tableDialog"
    :visible.sync="dialogVisible"
    width="80%"
    @close="close"
  >
    <el-form :model="formData" label-width="140px" :rules="formRules" ref="form">
      <el-row>
        <el-col>
          <el-form-item label="申请理由" prop="reason">
            <el-input type="textarea" v-model="formData.reason" placeholder="请输入申请原因"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col>
          <el-form-item label="配备方式" prop="pbtype">
            <el-select v-model="formData.pbtype" placeholder="请选择配备方式" style="width: 100%;">
              <el-option
                :label="item.name"
                :value="item.id"
                v-for="(item, index) in pbtypes"
                :key="index"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row v-if="formData.pbtype==1">
        <el-col>
          <el-form-item label="预算指标文件号" prop="ysno">
            <el-input v-model="formData.ysno" placeholder="请输入预算指标文件号"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="编制类型" prop="bzid">
            <el-select v-model="formData.bzid" placeholder="请选择编制类型" style="width: 100%;">
              <el-option
                :label="item.name"
                :value="item.id"
                v-for="(item, index) in bzids"
                :key="index"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车辆类型" prop="cartypeid">
            <el-select v-model="formData.cartypeid" placeholder="请选择车辆类型" style="width: 100%;">
              <el-option
                :label="item.name"
                :value="item.id"
                v-for="(item, index) in cartypeids"
                :key="index"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row v-if="formData.pbtype==1">
        <el-col>
          <el-form-item label="财政预算指标文件" prop="fileurl2">
            <el-upload
              class="upload-mini"
              drag
              :action="webUploadUrl"
              :on-success="onSuccess2"
              :limit="1"
              :on-exceed="onExceed"
              :on-remove="onRemove2"
              :headers="{'Authorization':token}"
            >
              <div class="el-upload__text">
                将文件拖到此处，或
                <em>点击上传</em>
              </div>
            </el-upload>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col>
          <el-form-item label="上传附件" prop="fileurl1">
            <el-upload
              class="upload-mini"
              drag
              :action="webUploadUrl"
              :on-success="onSuccess1"
              :limit="1"
              :on-exceed="onExceed"
              :on-remove="onRemove1"
              :headers="{'Authorization':token}"
            >
              <div class="el-upload__text">
                将文件拖到此处，或
                <em>点击上传</em>
              </div>
            </el-upload>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer" style="text-align: left;">
      <el-button type="primary" @click="save">确认</el-button>
      <el-button @click="resetForm">重置</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getProperties } from "@/api/dictionary";
import { apply } from "@/api/carpb";
import { mapGetters } from "vuex";
export default {
  data: function() {
    return {
      dialogVisible: false,
      pbtypes: [
        { name: "调拨", id: 0 },
        { name: "新购", id: 1 }
      ],
      formData: {
        pbtype: 0,
        fileurl1: null,
        filename1: null,
        fileurl2: null,
        filename2: null,
        reason: null,
        bzid: null,
        cartypeid: null,
        ysno: null
      },
      webUploadUrl: "", //文件上传地址
      bzids: [], //编制类型
      cartypeids: [], //车辆类型
      //表单验证
      formRules: {
        reason: [
          {
            required: true,
            message: "请输入申请原因",
            trigger: "blur"
          }
        ],
        pbtype: [
          {
            required: true,
            message: "请选择",
            trigger: "blur"
          }
        ],
        bzid: [
          {
            required: true,
            message: "请选择",
            trigger: "blur"
          }
        ],
        cartypeid: [
          {
            required: true,
            message: "请选择",
            trigger: "blur"
          }
        ],
        fileurl1: [
          {
            required: true,
            message: "请上传",
            trigger: "blur"
          }
        ],
        fileurl2: [
          {
            required: true,
            message: "请上传",
            trigger: "blur"
          }
        ],
        ysno: [
          {
            required: true,
            message: "请输入财政预算指标文件号",
            trigger: "blur"
          }
        ]
      }
    };
  },
  computed: {
    ...mapGetters(["userInfo", "token"])
  },
  created() {
    this.webUploadUrl = this.VUE_APP_BASE_API + "/common/webupload.shtml";
  },
  methods: {
    open() {
      this.dialogVisible = true;
      this.getProperties3();
      this.getProperties4();
    },
    onExceed() {
      this.$message("只能上传1个文件，请先移除已上传文件再上传");
    },
    onSuccess1(file) {
      if (!file) return;
      console.log(file);
      if (file.error == 0) {
        this.formData.fileurl1 = file.url;
        this.formData.filename1 = file.filename;
      } else {
        this.$message("上传失败，请重试");
      }
    },
    onRemove1() {
      this.formData.fileurl1 = "";
    },
    onSuccess2(file) {
      if (!file) return;
      console.log(file);
      if (file.error == 0) {
        this.formData.fileurl2 = file.url;
        this.formData.filename2 = file.filename;
      } else {
        this.$message("上传失败，请重试");
      }
    },
    onRemove2() {
      this.formData.fileurl2 = "";
    },
    //获取编制类型
    async getProperties3() {
      try {
        let r = await getProperties({ baseTypeId: 3 });
        if (r && r.error == 0) {
          this.bzids.splice(0, this.bzids.length, ...r.data);
          this.formData.bzid = r.data[0].id;
        } else {
          this.$message({
            message: "获取数据失败",
            type: "error"
          });
        }
      } catch (error) {
        this.$message({
          message: "获取数据失败",
          type: "error"
        });
      }
    },

    //获取车辆类型
    async getProperties4() {
      try {
        let r = await getProperties({ baseTypeId: 4 });
        if (r && r.error == 0) {
          this.cartypeids.splice(0, this.cartypeids.length, ...r.data);
          this.formData.cartypeid = r.data[0].id;
        } else {
          this.$message({
            message: "获取数据失败",
            type: "error"
          });
        }
      } catch (error) {
        this.$message({
          message: "获取数据失败",
          type: "error"
        });
      }
    },
    resetForm() {
      this.$refs.form.resetFields();
    },
    save() {
      this.$refs.form.validate(async pass => {
        if (!pass) {
          return;
        }
        try {
          let r = await apply(this.formData);
          if (r && r.error == 0) {
            this.$message({
              message: "提交成功",
              type: "success"
            });
            this.close();
          } else {
            this.$message({
              message: "出错了",
              type: "error"
            });
          }
        } catch (error) {
          this.$message({
            message: "出错了",
            type: "error"
          });
        }
      });
    },
    close() {
      this.dialogVisible = false;
      this.$parent.fetchData();
    }
  }
};
</script>

<style lang="scss" scoped>
.s_title {
  background: #f6f6f6;
  height: 40px;
  line-height: 40px;
  padding-left: 10px;
  margin-top: 0;
}
</style>